package com.wispx.shortlink.project.initialize;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 短链接监控 stream 初始化
 */
@Component
@RequiredArgsConstructor
public class ShortLinkStreamStatsInitializeTask implements InitializingBean {

    private final StringRedisTemplate stringRedisTemplate;

    @Value("${spring.data.redis.channel-topic.short-link-stats}")
    private String topic;

    @Value("${spring.data.redis.channel-topic.short-link-stats-group}")
    private String group;

    @Override
    public void afterPropertiesSet() {
        Boolean hasKey = stringRedisTemplate.hasKey(topic);
        if(hasKey == null || !hasKey){
            stringRedisTemplate.opsForStream().createGroup(topic,group);
        }
    }
}
